package top.tenebrous.yzl.chat.utils;

import top.tenebrous.yzl.chat.model.MemberLoginInfo;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author yazhang6
 */
public class ThreadLocalUtil {

    public ThreadLocalUtil() {

    }

    public static final ThreadLocal<Map<String, Object>> YZL_CONTEX = new ThreadLocal<>();

    /**
     * 存入线程变量
     */
    public static void put(String key, Object o) {
        //从ThreadLocal取出数据
        Map<String, Object> map = YZL_CONTEX.get();
        //判断是否为空
        if (map == null) {
            map = new HashMap<>();
            //把map放入到ThreadLocal中
            YZL_CONTEX.set(map);
        }
        //在map中存放数据
        map.put(key, o);
    }

    /**
     * 取出线程变量
     */
    public static Object get(String key) {
        Map<String, Object> map = YZL_CONTEX.get();
        return map != null ? map.get(key) : null;
    }

    /**
     * 移除当前线程指定的变量
     */
    public static void remove(String key) {
        Map<String, Object> map = YZL_CONTEX.get();
        map.remove(key);
    }

    /**
     * 移除当前线程全部的变量
     */
    public static void clear() {
        YZL_CONTEX.remove();
    }
}
